package co.rcantor.dev.siasar.visores;

import java.util.List;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import co.rcantor.dev.siasar.R;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class VisorMapa extends MapActivity {

	MapView mapView;
	List<Overlay> mapOverlays;
	Drawable drawable;
	MapItemizedOverlay itemizedOverlay;
	MapController controlMapa;
	Button btnSatelite = null;
	Button btnStreet = null;
	SQLiteDatabase db;
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
        
        btnSatelite = (Button)findViewById(R.id.BtnSatelite);
        btnStreet = (Button)findViewById(R.id.BtnStreet);      
        
        Double[][] coords = null;
        
        db = SQLiteDatabase.openDatabase("/data/data/co.rcantor.dev.siasar/databases/SIASARDB",null, SQLiteDatabase.OPEN_READWRITE);
        Cursor c = db.rawQuery("SELECT latitud,longitud FROM comunidades",	null);
        if (db != null) {			
			if (c != null) {
				coords=new Double[c.getCount()][2];
				int count =0;
				while (c.moveToNext()){
					coords[count][0]=c.getDouble(0);
					coords[count][1]=c.getDouble(1);
					count++;
				}
			}
		}
		db.close();
		
		mapView = (MapView) findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(true);
		
		controlMapa = mapView.getController();
        
        Double latitud = 9.535749*1E6;
		Double longitud = -82.397461*1E6;
		
		GeoPoint loc = 	new GeoPoint(latitud.intValue(), longitud.intValue());
		
		controlMapa.setCenter(loc);
        
        btnSatelite.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				if(mapView.isSatellite())
					mapView.setSatellite(false);
				else
					mapView.setSatellite(true);
			}
		});
        
        btnStreet.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				if(mapView.isStreetView())
					mapView.setStreetView(false);
				else
					mapView.setStreetView(true);
			}
		});

		
		mapOverlays = mapView.getOverlays();
		drawable = this.getResources().getDrawable(R.drawable.androidmarker);
		itemizedOverlay = new MapItemizedOverlay(drawable);
		
		GeoPoint point = null;
		OverlayItem overlayitem = null;
		
		if(c!=null){
			for (int i = 0; i < coords.length; i++) {
				point = new GeoPoint((int)(coords[i][0]* 1E6), (int)(coords[i][1] * 1E6));
				overlayitem = new OverlayItem(point, "Comunidad "+i, "");
				itemizedOverlay.addOverlay(overlayitem);
			}
			mapOverlays.add(itemizedOverlay);
		}
		
	}
	
	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
	
}
